import configparser

from configs.setting import FILE_PATH


class ConfigParse:
    """
    解析.ini后缀的配置文件
    """

    def __init__(self,file_path = FILE_PATH["ini"]):
        self.file_path = file_path
        self.config = configparser.ConfigParser()
        self.read_config()

    def read_config(self):
        self.config.read(self.file_path)

    def get_value(self,section,option):
        """
        获取配置文件的值
        :param section: 头参数
        :param option: 下级参数key值
        :return:
        """
        try:
            return self.config.get(section,option)
        except Exception as e:
            print(f"解析配置文件出现异常，原因：{e}")

    def get_host(self,option):
        """
        专门获取接口服务器ip地址信息
        :param option:
        :return:
        """
        return self.get_value("Host",option)

    def get_mysql_config(self,option):
        """
        获取mysql数据库的配置参数值
        :param option:
        :return:
        """
        return self.get_value("mysql",option)

if __name__ == '__main__':
    conf = ConfigParse()
    res = conf.get_host("host")
    res_mysql_host = conf.get_mysql_config("host")
    print(res_mysql_host)